<?php

/**
 * 小程序服务商管理系统 v1.0.1
 *
 * 商业运营到axzz.cn官网进行授权使用。
 *
 * (c) cighsen02 <xiayu@959602.com>
 *
 * Create at：2025.05.03
 * Update at：2025.07.09
*/

namespace App\Http\Controllers\Auth;

use App\Exceptions\JsonResponseException;
use App\Http\Controllers\Controller;
use App\Services\Auth\User as UserService;
use App\Services\Auth\ValidatorUser;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
use Illuminate\Validation\ValidationException;

/**
 * 用户管理
 */
class UserController extends Controller implements HasMiddleware
{
    /**
     * 权限中间件
     *
     * @return array
     */
    public static function middleware(): array
    {
         return [
            'auth:user',
            new Middleware('permission:role-list|role-create|role-edit|role-delete', only: ['index','show']),
            new Middleware('permission:role-create', only: ['store']),
            new Middleware('permission:role-edit', only: ['update']),
            new Middleware('permission:role-delete', only: ['destroy'])
        ];
    }

    /**
     * 列表
     *
     * @return JsonResponse
     */
    public function index(UserService $service): JsonResponse
    {
        return $this->sendResponse($service->index(), 'success');
    }

    /**
     * 更新
     *
     * @param Request $request
     * @param ValidatorUser $validator
     * @param UserService $service
     * @return JsonResponse
     */
    public function store(Request $request, ValidatorUser $validator, UserService $service): JsonResponse
    {
        try{

            $validated  = $validator->store($request->all());
            $result     = $service->store($validated);
        }
        catch (ValidationException $e) {

            throw new JsonResponseException(implode(',', $e->errors()), 422);
        }

        return $this->sendResponse($result, 'success');
    }

    /**
     * 更新 path参数同名，服务层改为UserService
     *
     * @param UserService $service
     * @param mixed $user
     * @return JsonResponse
     */
    public function show(UserService $service, $user): JsonResponse
    {
        try{

            $result = $service->show($user);
        }
        catch (ValidationException $e) {

            throw new JsonResponseException(implode(',', $e->errors()), 422);
        }

        return $this->sendResponse($result, 'success');
    }

    /**
     * 更新
     *
     * @param Request $request
     * @param ValidatorUser $validator
     * @param UserService $service
     * @param mixed $user
     * @return JsonResponse
     */
    public function update(Request $request, ValidatorUser $validator, UserService $service, $user): JsonResponse
    {
        try{

            $validated  = $validator->update($request->all());
            $result     = $service->update($validated, $user);
        }
        catch (ValidationException $e) {

            throw new JsonResponseException(implode(',', $e->errors()), 422);
        }

        return $this->sendResponse($result, 'success');
    }

    /**
     * 删除
     *
     * @param UserService $service
     * @param mixed $user
     * @return JsonResponse
     */
    public function destroy(UserService $service, $user): JsonResponse
    {
        $service->destroy($user);

        return $this->sendResponse([], 'success');
    }
}
